// Learn TypeScript:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/life-cycle-callbacks.html

import StartUp from "../../../lib/StartUp";
import CM from "../../../lib/cache/CM";
import { monitor, selfCls } from "../../../lib/common/Decorator";
import { Manager } from "../../../lib/common/Manager";
import DBMgr from "../../../lib/db/DBMgr";
import GameObject from "../../../lib/object/GameObject";
import DBattle from "../../cache/DBattle";
import DBPlay from "../../config/DBPlay";
import GOPlay from "../play/GOPlay";


@selfCls
export default class PlayManager extends Manager {
    get parentRoot(): cc.Node {
        return StartUp.instance.gameRoot;
    }

    public do(): Promise<void> {
        return new Promise<void>((resolve, reject)=>{
            resolve();
        })
    }
    @monitor("DBattle/playId")
    on_playId_change(attrName, oldVal, newVal) {
        let script = DBPlay.playScriptName(newVal);
        this.createPlay(script);
        console.warn("切换玩法 旧的:"+oldVal+" 新的:"+newVal);
    }

    public createPlay<T extends GOPlay>(name:string):T {
        let go = new cc.Node(name);
        let cls = cc.js.getClassByName(name) as any
        let play = go.addComponent(cls) as GameObject;
        play.init();
        this.node.addChild(go);
        return play as T;
    }
}
